import numpy as np
import matplotlib.pyplot as plt
import time
import cv2

def curr_zt(day_start_hr):
	tms = time.localtime(time.time())
	zt = tms.tm_hour + tms.tm_min/60.0 + tms.tm_sec/3600.0 - day_start_hr
	if zt < 0:
		zt += 24.0
	return zt

def preview(fig_name = 'preview'):
	cam_h = cv2.VideoCapture(0)
	for i in range(10):
		ret, img_fr = cam_h.read()
	ret, img_fr = cam_h.read()
	cv2.imwrite('%s.png' % fig_name, img_fr)
	print('Saving image %s.png ...... ' % fig_name, end='')
	#print(img_fr.shape)
	#fig = plt.figure()
	#plt.imshow(img_fr)
	#plt.title(fig_name)
	#plt.show()
	#plt.close(fig)

def routine_preview():
	day_start_hr = 9.0
	zthr = -1
	zt = curr_zt(day_start_hr)
	fig_idx = 0
	while True:
		zt = curr_zt(day_start_hr)
		if zt - zthr < 0 or zt - zthr > 1:
			zthr = int(zt)
			fig_idx += 1
			preview('fig_%d_zt-%fhr' % (fig_idx, zt))
			print('done')
		time.sleep(1.01)
	
routine_preview()